<template>
  <div class="departments">
    <div class="app-container">
      <el-card class="box-card">
        <template #header>
          <span>
            <!-- 用一个行列布局 -->
            <el-row>
              <el-col :span="20">
                <span>{{ companyName }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>负责人</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown v-if="canClick('departments')">
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="openAddDept()">添加顶级部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </span>
        </template>
        <div>
          <el-tree :data="treeData" :props="defaultProps" default-expand-all>
            <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
            <!--
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (table 单元格数据渲染)
    -->
            <template #default="{ data }">
              <el-row style="width: 100%;">
                <el-col :span="20">
                  <span>{{ data.name }}</span>
                </el-col>
                <el-col :span="4">
                  <el-row type="flex" justify="end">
                    <!-- 两个内容 -->
                    <el-col>{{ data.manager }}</el-col>
                    <el-col>
                      <!-- 下拉菜单 element -->
                      <el-dropdown>
                        <span> 操作<i class="el-icon-arrow-down" /> </span>
                        <!-- 下拉菜单 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="openAddDept(data, 1)">添加子部门</el-dropdown-item>
                          <el-dropdown-item @click.native="openAddDept(data, 2)">编辑部门</el-dropdown-item>
                          <el-dropdown-item @click.native="delDept(data)">删除部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-tree>
        </div>
      </el-card>
    </div>
    <!-- 弹层放到根元素结束之前 -->
    <AddDept ref="ad" :show-dialog="showDialog" :all-depts="allDepts" :parent-dept="parentDept" @refresh-dept="getTreeData" @close-dialog="showDialog = $event" />

  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transformTreeData } from '@/utils'
import AddDept from './compoments/add-dept.vue'
export default {
  components: {
    AddDept
  },
  data () {
    return {
      showDialog: false,
      treeData: [],
      allDepts: [],
      companyName: '',
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      parentDept: null
    }
  },
  created () {
    this.getTreeData()
  },
  methods: {

    // 打开新增部门弹层方法
    // parentDept 添加子部门的父部门数据 =》说明：顶级部门没有父，所以parentDept是undefined
    openAddDept (parentDept, type) {
      console.log(parentDept)
      // 1. 存储子部门的父部门
      this.parentDept = parentDept
      // 2. 打开弹层
      this.showDialog = true
      if (type === 2) {
        this.$refs.ad.getDeptDetil(parentDept.id)
      }
    },
    async getTreeData () {
      const { depts, companyName } = await getDepartments()
      // console.table(depts)
      // console.log(transformTreeData(depts))
      this.allDepts = depts
      this.treeData = transformTreeData(depts)
      this.companyName = companyName
    },
    async delDept (curDept) {
      try {
        await this.$confirm(`确定要删除:${curDept.name}吗？`)
        if (curDept.children && curDept.children.length > 0) {
          return this.$message.error('不能直接删除父部门')
        }
        console.log(123)
        await delDepartments(curDept.id)
        this.getTreeData()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    }

  }

}
</script>

<style lang="sass" scoped>
::v-deep .el-tree-node__content
  margin: 3px 0
  font-size: 14px
</style>
